<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2066 – Since you double quoted this, it will not word split, and the loop will only run once.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2066 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2066">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="since-you-double-quoted-this-it-will-not-word-split-and-the-loop-will-only-run-once">Since
you double quoted this, it will not word split, and the loop will only
run once.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2066.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> s <span class="kw">in</span> <span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycommand</span><span class="va">)</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$s</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<p>The correct code depends on your intention. Let's say you're in a
directory with the files <code>file.png</code> and
<code>My cat.png</code>, and you want to loop over a command that
outputs (or variable that contains):</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2066.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hello</span> world</span>
<span id="cb2-2"><a href="SC2066.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ex">My</span> <span class="pp">*</span>.png</span></code></pre></div>
<h4 id="loop-over-each-line-without-globbing-hello-world-my-png">Loop
over each line without globbing (<code>hello world</code>,
<code>My *.png</code>)</h4>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2066.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">mycommand</span> <span class="kw">|</span> <span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span> <span class="bu">read</span> <span class="at">-r</span> <span class="va">s</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$s</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<h4
id="loop-over-each-word-with-globbing-hello-world-my-filepng-my-catpng">Loop
over each word with globbing (<code>hello</code>, <code>world</code>,
<code>My</code>, <code>file.png</code>, <code>My cat.png</code>):</h4>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2066.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># relies on the fact that IFS by default contains space-tab-linefeed</span></span>
<span id="cb4-2"><a href="SC2066.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> s <span class="kw">in</span> <span class="va">$(</span><span class="ex">mycommand</span><span class="va">)</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$s</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<h4 id="loop-over-each-line-with-globbing-hello-world-my-catpng">Loop
over each line with globbing (<code>hello world</code>,
<code>My cat.png</code>)</h4>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="SC2066.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># explicitly set IFS to contain only a line feed</span></span>
<span id="cb5-2"><a href="SC2066.html#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="va">IFS</span><span class="op">=</span><span class="st">&#39;</span></span>
<span id="cb5-3"><a href="SC2066.html#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="st">&#39;</span></span>
<span id="cb5-4"><a href="SC2066.html#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> s <span class="kw">in</span> <span class="va">$(</span><span class="ex">mycommand</span><span class="va">)</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$s</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You get this warning because you have a loop that will only ever run
exactly one iteration. Since you have a loop, you clearly expect it to
run more than once. You just have to decide how it should be split
up.</p>
<h3 id="exceptions">Exceptions</h3>
<p>None.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


